﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.VisualBasic;
using System.Windows.Forms;

namespace HabboClone
{
    class Map
    {
        public Texture2D GroundText;
        public Texture2D GroundTextOver;
        public float gameWidth;
        public float gameHeight;
        public Tile[,] Tiles;
        int spielFeldGroesse = 15;
        public Map(ContentManager content)
        {
            this.GroundText = content.Load<Texture2D>("ground");
            this.GroundTextOver = content.Load<Texture2D>("groundover");
            Tiles = new Tile[spielFeldGroesse, spielFeldGroesse];
            
            for (int x = 0; x < spielFeldGroesse; x++)
            {
                for (int y = 0; y < spielFeldGroesse; y++)
                {
                    Tiles[x, y] = new Tile(GroundText, "Ground", new Point(x,y) ,true);
                }
            }
            /*List<String> lines = File.ReadAllLines("map.txt").ToList();
            Vector2 MapSize = new Vector2(int.Parse(lines[0].Split('|')[0]), int.Parse(lines[0].Split('|')[1]));
            lines.RemoveAt(0);
            Tiles = new Texture2D[(int)MapSize.X, (int)MapSize.Y];
            
            foreach(String line in lines)
            {
                int TilePosX = int.Parse(line.Split('|')[0]);
                int TilePosY = int.Parse(line.Split('|')[1]);
                
            }*/

        }
        public void Update(GameTime gametime)
        {
            for (int x = 0; x < spielFeldGroesse; x++)
            {
                for (int y = 0; y < spielFeldGroesse; y++)
                {
                    if (Tiles[x, y].Texture == GroundTextOver)
                    {
                        Tiles[x, y].Texture = GroundText;
                    }
                }
            }
            Dictionary<Rectangle, Point> boundingBoxes = new Dictionary<Rectangle, Point>();
            Dictionary<float, Point> differenceBetweenMouse = new Dictionary<float, Point>();
            MouseState mState = Mouse.GetState();
            for (int x = 0; x < spielFeldGroesse; x++)
            {
                for (int y = 0; y < spielFeldGroesse; y++)
                {

                    if (Tiles[x, y].Name == "Ground")
                    {
                        float position_x_ = (gameWidth / 2 - Tiles[x, y].Texture.Width / 2) - y * Tiles[x, y].Texture.Width / 2 + x * Tiles[x, y].Texture.Width / 2;
                        float position_y_ = (gameHeight / 2 - Tiles[x, y].Texture.Height / 2 * spielFeldGroesse) + y * Tiles[x, y].Texture.Height / 2 + x * Tiles[x, y].Texture.Height / 2;
                        Rectangle relativeTilePos = new Rectangle((int)position_x_, (int)position_y_, Tiles[x, y].Texture.Width, Tiles[x, y].Texture.Height);

                        if (relativeTilePos.Contains(new Point(mState.X, mState.Y)) == true)
                        { 
                            boundingBoxes.Add(relativeTilePos, new Point(x,y));
                        }
                    }
                }
            }
            
            foreach (KeyValuePair<Rectangle, Point> boundBox in boundingBoxes)
            {
                Rectangle tile = boundBox.Key;
                
                //Differenz ausrechnen
                float differenceTileMouse = (float)Math.Sqrt(Math.Pow(tile.X - mState.X, 2) + Math.Pow(tile.Y - mState.Y, 2));
                differenceBetweenMouse.Add(differenceTileMouse, boundBox.Value);
                //zu einer Liste hinzufügen, um zu sortieren
            }
            if (differenceBetweenMouse.Count != 0)
            {
                List<float> differencesRaw = new List<float>();
                foreach (KeyValuePair<float, Point> differences in differenceBetweenMouse)
                {
                    //um wirklich sortieren zu können, müssen nochmal die float-Werte inne list kopiert werden
                    differencesRaw.Add(differences.Key);

                }
                differencesRaw.Sort();
                differencesRaw.Reverse();
                Tiles[differenceBetweenMouse[differencesRaw[0]].X, differenceBetweenMouse[differencesRaw[0]].Y].Texture = GroundTextOver;
          
            }


        }

        public void Draw(SpriteBatch spritebatch, GameTime gametime)
        {
            // draw the main grid
            for (int x = 0; x < spielFeldGroesse; x++)
            {
                for (int y = 0; y < spielFeldGroesse; y++)
                {
                    Tile currentTile = Tiles[x, y];

                    float position_x_ = (gameWidth / 2 - currentTile.Texture.Width / 2) - y * currentTile.Texture.Width / 2 + x * currentTile.Texture.Width / 2;
                    float position_y_ = (gameHeight / 2 - currentTile.Texture.Height / 2 * spielFeldGroesse) + y * currentTile.Texture.Height / 2 + x * currentTile.Texture.Height / 2;
                    spritebatch.Draw(currentTile.Texture, new Vector2(position_x_, position_y_), Color.White);
                }
            }

        }
    }
}
